package com.xh.car.controller;

import com.xh.car.bean.Apply;
import com.xh.car.bean.Userinfo;
import com.xh.car.service.ApplyService;
import com.xh.car.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @Author PangJunjie
 * @Date 2024/5/14/014
 */
@Controller
@RequestMapping("/apply")
public class ApplyController {
    @Autowired
    private ApplyService applyService;

    /**
     * 申请用车页
     *
     * @return
     */
    @GetMapping("")
    public String index(String type, Model model) {
        model.addAttribute("type", type);
        return "apply/apply";
    }

    /**
     * 所有申请列表页
     *
     * @return
     */
    @GetMapping("/all")
    public String applyAll() {
        return "apply/apply-all";
    }

    /**
     * 我的申请列表页
     *
     * @return
     */
    @GetMapping("/list")
    public String applyList() {
        return "apply/apply-list";
    }

    /**
     * 待审核页面
     *
     * @return
     */
    @GetMapping("/exam")
    public String applyExam() {
        return "apply/apply-exam";
    }

    @GetMapping("/return")
    public String applyReturn() {
        return "apply/apply-return";
    }

    @GetMapping("/{id}")
    @ResponseBody
    public R apply(@PathVariable int id) {
        return R.data(applyService.getById(id));
    }


    @GetMapping("/list/page")
    @ResponseBody
    public R listPage(int page, int limit, Apply apply, String type, HttpSession session) {
        Userinfo loginInfo = (Userinfo) session.getAttribute("loginInfo");
        apply.setApplyId(loginInfo.getId());
        if (loginInfo.getRole() == 2) {
            apply.setApplyStatus(3);
        } else if (loginInfo.getRole() == 3) {
            apply.setApplyStatus(1);
        }
        return applyService.getPage(page, limit, apply);
    }


    @GetMapping("/exam/page")
    @ResponseBody
    public R examPage(int page, int limit, Apply apply, String type, HttpSession session) {
        Userinfo loginInfo = (Userinfo) session.getAttribute("loginInfo");
        if (loginInfo.getRole() == 1) {
            apply.setApplyStatus(3);
        } else if (loginInfo.getRole() == 2) {
            apply.setApplyStatus(1);
            apply.setDepartmentId(loginInfo.getDepartmentId());
        }
        return applyService.getPage(page, limit, apply);
    }

    @GetMapping("/all/page")
    @ResponseBody
    public R page(int page, int limit, Apply apply, HttpSession session) {
        Userinfo loginInfo = (Userinfo) session.getAttribute("loginInfo");
        if (loginInfo.getRole() == 2) {
            apply.setDepartmentId(loginInfo.getDepartmentId());
        } else if (loginInfo.getRole() == 3) {
            apply.setApplyId(loginInfo.getId());
        } else if (loginInfo.getRole() == 4) {
            apply.setDriverId(loginInfo.getId());
        }

        return applyService.getPage(page, limit, apply);
    }

    @GetMapping("/return/page")
    @ResponseBody
    public R returnPage(int page, int limit, Apply apply, HttpSession session) {
        apply.setApplyStatus(5);
        Userinfo userinfo = (Userinfo) session.getAttribute("loginInfo");
        if (userinfo.getRole() == 2 || userinfo.getRole() == 3) {
            apply.setApplyId(userinfo.getId());
        }
        if (userinfo.getRole() == 4) {
            apply.setDriverId(userinfo.getId());
        }
        return applyService.getPage(page, limit, apply);
    }

    @PostMapping("/save")
    @ResponseBody
    public R save(Apply apply, HttpSession session) {
        Userinfo userinfo = (Userinfo) session.getAttribute("loginInfo");
        apply.setApplyId(userinfo.getId());
        apply.setApplyTime(new Date());
        apply.setApplyStatus(1);
        if (userinfo.getRole() == 2) {
            apply.setApplyStatus(3);
            apply.setDepExamId(userinfo.getId());
            apply.setDepExamTime(new Date());
            apply.setDepContent("负责人申请，默认同意");
        }
        applyService.saveOrUpdate(apply);
        return R.ok();
    }

    @PostMapping("/update")
    @ResponseBody
    public R update(Apply apply) {
        applyService.saveOrUpdate(apply);
        return R.ok();
    }

    @PostMapping("/exam")
    @ResponseBody
    public R exam(Apply apply, HttpSession session) {
        Userinfo userinfo = (Userinfo) session.getAttribute("loginInfo");
        if (userinfo.getRole() == 1) {
            apply.setAdmExamId(userinfo.getId());
            apply.setAdmExamTime(new Date());
        } else if (userinfo.getRole() == 2) {
            apply.setDepExamId(userinfo.getId());
            apply.setDepExamTime(new Date());
        } else {
            return R.error("你无权审核！");
        }
        applyService.saveOrUpdate(apply);
        return R.ok();
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public R delete(@PathVariable int id) {
        applyService.delete(id);
        return R.ok();
    }
}
